//
//  pictureViewController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-25.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "pictureViewController.h"
#import "pictureCollectionCell.h"
#import "pictureModel.h"
#import "MJRefresh.h"
#import "pictureDetailController.h"

@interface pictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (assign, nonatomic) NSInteger count1;

@end

@implementation pictureViewController

- (void)dealloc
{
    [_collecArray release];
    [_collecView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collecArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationItem setTitle:@"精选图片"];
    //[self.view setBackgroundColor:[UIColor colorWithRed:155 / 255.0 green:186 / 255.0 blue:48 / 255.0 alpha:1]];
    [self createCollectionView];
    [self startAsync];
    [self setupRefresh];
}


#pragma mark - 
#pragma mark  创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [flow setItemSize:CGSizeMake((self.view.frame.size.width - 30) / 2, (self.view.frame.size.width - 30) / 2)];
    [flow setMinimumInteritemSpacing:10];
    [flow setMinimumLineSpacing:10];
    
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);

    self.collecView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    
    
    [_collecView setBackgroundColor:[UIColor colorWithRed:228 / 255.0 green:228 / 255.0  blue:228 / 255.0  alpha:1]];
    [_collecView registerClass:[pictureCollectionCell class] forCellWithReuseIdentifier:@"ok"];
    _collecView.delegate = self;
    _collecView.dataSource = self;
    [self.view addSubview:_collecView];
    [flow release];
    [_collecView release];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_collecArray count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    pictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ok" forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.layer.cornerRadius = 10;
    
    pictureModel *model = [_collecArray objectAtIndex:indexPath.item];
    cell.picModel = model;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    pictureDetailController *detail = [[pictureDetailController alloc] init];
    detail.num = indexPath.item;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)startAsync
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/featured/photos/?start=%ld&count=12", _count1]];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arr = [dic objectForKey:@"items"];
        for (NSMutableDictionary *picDic in arr) {
            pictureModel *model = [[pictureModel alloc] init];
            [model setValuesForKeysWithDictionary:picDic];
            [_collecArray addObject:model];
            [_collecView reloadData];
            [model release];
        }
    }];
}


- (void)setupRefresh
{
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.collecView addHeaderWithTarget:self action:@selector(headerRereshing)];
   // #warning 自动刷新(一进入程序就下拉刷新)
    [self.collecView headerBeginRefreshing];
    
     //2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.collecView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.collecView.headerPullToRefreshText = @"下拉可以刷新了";
    self.collecView.headerReleaseToRefreshText = @"松开立即刷新了";
    self.collecView.headerRefreshingText = @"正在刷新中";
    
    self.collecView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.collecView.footerReleaseToRefreshText = @"松开立即加载更多数据";
    self.collecView.footerRefreshingText = @"正在加载中";
}

- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self.collecView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.collecView headerEndRefreshing];
    });
}


- (void)footerRereshing
{
    
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self startAsync];
        [self.collecView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.collecView footerEndRefreshing];
    });
    
    _count1++;
    
}






@end
